import java.util.Scanner;

/**
 * Created by Administrator on 2022/8/2.
 */
public class Demo {
    public static void main(String[] args) {
        judgeBit1();//方法一：二进制中1的个数
        judgeBit2();//方法二：二进制中1的个数
        cumulativeSum();//正负交换求累加和
        narcissus1();//三位数水仙花数
        narcissus2();//多位数水仙花数
        printX();//打印图形x
        multiplicationTable();//打印乘法表
        print();//输出一位数中的每一位
        classify();//将一个数二进制奇偶分开
        simulatedLogin();//模拟登陆

    }
    public static void judgeBit1() {
        //求一个整数，在内存当中存储时，二进制1的个数。
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入一个数：");
        int n = scan.nextInt();
        int sum = 0;
        for (int i = 0; i < 32; i++) {
            if(((n >>> i) & 1) == 1){
                sum++;
            }
        }
        System.out.println(sum);
    }

    public static void judgeBit2() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入一个数：");
        int n = scan.nextInt();
        int sum = 0;
        int i = 0;
        while(n != 0 && i < 32){
            if(((n >> i) & 1 ) == 1){
                sum++;
            }
            i++;
        }
        System.out.println(sum);
    }


    public static void cumulativeSum() {
        //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
        int symbol = 1;
        double sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += (1.0 / i) * symbol;
            symbol = -symbol;
        }
        System.out.println(sum);
    }

    public static void narcissus1() {
        /*求出0～n之间的所有“水仙花数”并输出。
        (“水仙花数”是指一个三位数，其各位数字的立方和确好等于该数本身，
        如；153＝1^3＋5^3＋3^3，则153是一个“水仙花数“。）*/
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入n:");
        int n = scan.nextInt();
        for (int i = 0; i <= n ; i++) {
            if(i >= 100 && i < 1000){
                int g = i % 10;
                int s = i / 10 % 10;
                int b = i / 100 % 10;
                if(g * g * g + s * s * s + b * b * b == i){
                    System.out.println(i);
                }
            }
        }
    }

    public static void narcissus2() {
        //找自身数 类似于水仙花数
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入n:");
        int n = scan.nextInt();
        for (int i = 0; i <= n; i++) {
            int count = 0;
            int sum = 0;
            int tmp = i;
            while(tmp != 0){
                tmp = tmp / 10;
                count++;
            }
            tmp = i;
            while(tmp != 0){
                sum += Math.pow((tmp % 10),count);
                tmp = tmp / 10;
            }
            if(sum == i ){
                System.out.println(i);
            }
        }
    }

        public static void printX() {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) {
                int n = in.nextInt();
                for(int i = 0;i < n;i++){
                    for(int j = 0;j < n;j++){
                        if(i == j || j == n - 1 - i){
                            System.out.printf("*");
                        }else{
                            System.out.printf(" ");
                        }
                    }
                    System.out.printf("\n");
                }
            }
        }

    public static void multiplicationTable() {
        //乘法表
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i = 1; i <= n; i++){
            for (int j = 1; j <= i; j++) {
                System.out.printf("%d * %d = %d ",i,j,i*j);
            }
            System.out.println();
        }
    }

    public static void print() {
        //输出一个整数的每一位，如：123的每一位是3，2，1
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        while(n != 0){
            int r = n % 10;
            n = n / 10;
            System.out.println(r);
        }
    }

    public static void classify() {
        //获取一个数二进制序列中所有的偶数位和奇数位， 分别输出二进制序列
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i = 31;i >= 0;i -= 2){
            System.out.print((n >> i) & 1);
        }
        System.out.println();
        for(int j = 30; j >= 0; j -= 2) {
            System.out.print((n >> j) & 1);
        }
    }

    public static void simulatedLogin() {
       /* 编写代码模拟三次密码输入的场景。
        最多能输入三次密码，密码正确，提示“登录成功”,密码错误， 可以重新输 入，最多输入三次。
        三次均错，则提示退出程序*/
       Scanner scan = new Scanner(System.in);
       int count = 3;
        while(count != 0){
            System.out.println("有"+count+"输入机会");
            String str = scan.nextLine();
            if(str.equals("123")){
                System.out.println("密码正确");
                break;
            }else {
                System.out.println("密码错误重新输入");
                count--;
            }
        }
    }
}

